﻿using Bandit.Helpers;
using RD.UsbHid;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ETC.HallICElan2In1.Helpers
{
    public class USBHelper
    {

        public delegate void MyConnectedHandler(bool isConnect);

        public event MyConnectedHandler OnMyConnectedChanged;

        public delegate void MyDataReceivedHandler(object sender, HidReveivedData e);

        public event MyDataReceivedHandler OnMyDataReceivedChanged;

        private int _VendorID;

        private int _ProductID;

        private UsbHidDevice _UsbHidDevice;

        private AutoResetEvent _AutoResetEvent = new AutoResetEvent(false);

        private bool _IsConnect;
        public bool IsConnect
        {
            get { return _IsConnect; }
            set { _IsConnect = value; }
        }

        private byte[] _ReceviceData;


        public USBHelper()
        {
            _VendorID = 0x2019;
            _ProductID = 0x1314;
        }

        public USBHelper(int vid, int pid)
        {

            _VendorID = vid;
            _ProductID = pid;
        }

        private static USBHelper _Instance;

        public static USBHelper Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new USBHelper();

                }
                return _Instance;
            }
            set { _Instance = value; }
        }

        public void Connect()
        {
            _UsbHidDevice = new UsbHidDevice(_VendorID, _ProductID);

            _UsbHidDevice.OnConnectedChanged += _UsbHidDevice_OnConnectedChanged;

            _UsbHidDevice.OnError += _UsbHidDevice_OnError;

            if (_UsbHidDevice.Connect())
            {
                IsConnect = true;
                if (this.OnMyConnectedChanged != null)
                    //this.OnMyConnectedChanged(IsConnect);
                    _UsbHidDevice.OnDataReceived += _UsbHidDevice_OnDataReceived;
            }
            else
            {
                IsConnect = false;
                if (this.OnMyConnectedChanged != null)
                    this.OnMyConnectedChanged(IsConnect);
                _UsbHidDevice.OnDataReceived -= _UsbHidDevice_OnDataReceived;
            }

        }

        public void Disconnect()
        {
            if (_UsbHidDevice != null)
            {
                _UsbHidDevice.OnConnectedChanged -= _UsbHidDevice_OnConnectedChanged;
                _UsbHidDevice.OnDataReceived -= _UsbHidDevice_OnDataReceived;
                _UsbHidDevice.OnError -= _UsbHidDevice_OnError;
                _UsbHidDevice.Disconnect();
            }

        }

        private void _UsbHidDevice_OnError(object sender, Exception ex)
        {
            Console.WriteLine("☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆");
            Console.WriteLine();
            Console.WriteLine(ex.ToString());
            Console.WriteLine();
            Console.WriteLine("☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆");
        }

        private void _UsbHidDevice_OnDataReceived(object sender, HidReveivedData e)
        {
            byte[] data = e.Data;

           // LogHelper.Info(">>>USB Rec:" + string.Join(" ", data));

            OnMyDataReceivedChanged(sender, e);
           
        }

        private void _UsbHidDevice_OnConnectedChanged(object sender, bool isConnect)
        {
            IsConnect = isConnect;

            if (this.OnMyConnectedChanged != null)
            {
                this.OnMyConnectedChanged(isConnect);
            }
        }

        private byte[] UsbSendCommand(byte[] bytes)
        {
            this._ReceviceData = new byte[] { };

            _UsbHidDevice.SendMessage(bytes);

            _AutoResetEvent.Reset();
            _AutoResetEvent.WaitOne(2000);

            return _ReceviceData;

        }

        public bool SendPowerOn()
        {
            LogHelper.Info("上电操作");

            if (this.IsConnect)
            {
                byte[] bytes1 = new byte[] { 0x0C, 0xFA, 0x01, 0x00, 0xFF };

                byte[] bytes = UsbSendCommand(bytes1);

                //if (bytes.Count() > 0)
                //{
                LogHelper.Info("上电成功");
                //    return true;
                //}
                //else
                //{
                //    LogHelper.Info("上电失败");
                //    return false;
                //}
                return true;
            }

            //LogHelper.Info("上电失败");

            return false;
        }

        public bool SendPowerOff()
        {
            LogHelper.Info("下电操作");

            if (IsConnect)
            {
                byte[] bytes1 = new byte[] { 0x0C, 0xFA, 0x02, 0x00, 0xFF };

                byte[] bytes = UsbSendCommand(bytes1);


                //if (bytes.Count() > 0) {

                LogHelper.Info("下电成功");

                //    return true;
                //}
                //else
                //{
                //    LogHelper.Info("下电失败");
                //    return false;
                //}

                return true;
            }

            LogHelper.Info("下电失败");

            return false;
        }
    }
}
